@echo off
title 物业保洁系统 - 全套部署环境安装
color 0A
echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                  物业保洁系统安装程序                        ║
echo ║              Windows 10/11 全套部署环境                    ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.
echo 正在检测系统环境...
echo ================================================

REM 检查管理员权限
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo ❌ 需要以管理员身份运行此脚本
    echo 请右键点击此文件，选择"以管理员身份运行"
    pause
    exit /b 1
)

REM 创建安装目录
set INSTALL_DIR=C:\PropertySystem
mkdir "%INSTALL_DIR%" 2>nul
cd /d "%INSTALL_DIR%"

echo ✅ 安装目录: %INSTALL_DIR%
echo.

REM 安装Chocolatey（如果未安装）
echo 正在安装包管理器...
if not exist "%PROGRAMDATA%\chocolatey" (
    echo 正在安装Chocolatey...
    powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
    timeout /t 10 /nobreak >nul
) else (
    echo ✅ Chocolatey已安装
)

REM 刷新环境变量
call refreshenv 2>nul || (
    echo 正在刷新环境变量...
    set PATH=%PATH%;%PROGRAMDATA%\chocolatey\bin
)

echo.
echo 正在安装必要软件...
echo ================================================

REM 安装Git
choco install git -y --force
if %errorlevel% neq 0 (
    echo ❌ Git安装失败
) else (
    echo ✅ Git安装完成
)

REM 安装Docker Desktop
echo 正在安装Docker Desktop...
choco install docker-desktop -y --force
if %errorlevel% neq 0 (
    echo ❌ Docker安装失败
) else (
    echo ✅ Docker安装完成
)

REM 安装Java 17
echo 正在安装Java 17...
choco install openjdk17 -y --force
if %errorlevel% neq 0 (
    echo ❌ Java安装失败
) else (
    echo ✅ Java安装完成
)

REM 安装Node.js
echo 正在安装Node.js...
choco install nodejs -y --force
if %errorlevel% neq 0 (
    echo ❌ Node.js安装失败
) else (
    echo ✅ Node.js安装完成
)

REM 安装开发工具
echo.
echo 正在安装开发工具...
echo ================================================

choco install vscode -y --force
choco install postman -y --force
choco install docker-compose -y --force
choco install 7zip -y --force
choco install notepadplusplus -y --force

echo.
echo 正在创建快捷方式...
echo ================================================

REM 创建桌面快捷方式
set DESKTOP=%USERPROFILE%\Desktop
set SCRIPTS_DIR=%INSTALL_DIR%\scripts
mkdir "%SCRIPTS_DIR%" 2>nul

REM 创建启动脚本
echo @echo off > "%SCRIPTS_DIR%\start-system.bat"
echo title 物业保洁系统 - 启动服务 >> "%SCRIPTS_DIR%\start-system.bat"
echo color 0A >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 正在启动物业保洁系统... >> "%SCRIPTS_DIR%\start-system.bat"
echo echo ================================================ >> "%SCRIPTS_DIR%\start-system.bat"
echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo REM 检查Docker >> "%SCRIPTS_DIR%\start-system.bat"
echo docker --version ^>nul 2^>^&1 >> "%SCRIPTS_DIR%\start-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\start-system.bat"
echo     echo ❌ Docker未安装或未运行 >> "%SCRIPTS_DIR%\start-system.bat"
echo     pause >> "%SCRIPTS_DIR%\start-system.bat"
echo     exit /b >> "%SCRIPTS_DIR%\start-system.bat"
echo ) >> "%SCRIPTS_DIR%\start-system.bat"
echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo REM 等待Docker就绪 >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 等待Docker服务就绪... >> "%SCRIPTS_DIR%\start-system.bat"
echo :waitfordocker >> "%SCRIPTS_DIR%\start-system.bat"
echo docker info ^>nul 2^>^&1 >> "%SCRIPTS_DIR%\start-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\start-system.bat"
echo     timeout /t 5 /nobreak ^>nul >> "%SCRIPTS_DIR%\start-system.bat"
echo     goto waitfordocker >> "%SCRIPTS_DIR%\start-system.bat"
echo ) >> "%SCRIPTS_DIR%\start-system.bat"
echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo REM 启动系统 >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 正在启动物业保洁系统... >> "%SCRIPTS_DIR%\start-system.bat"
echo cd /d "C:\Users\Administrator\Documents\traeWorkArea\knowledge-base-generator-doubao-thinking\property-cleaning-system" >> "%SCRIPTS_DIR%\start-system.bat"
echo docker-compose up -d >> "%SCRIPTS_DIR%\start-system.bat"
echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo REM 检查服务状态 >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 检查服务状态... >> "%SCRIPTS_DIR%\start-system.bat"
echo timeout /t 10 /nobreak ^>nul >> "%SCRIPTS_DIR%\start-system.bat"
echo docker-compose ps >> "%SCRIPTS_DIR%\start-system.bat"
echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 系统启动完成！ >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 访问地址： >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 前端: http://localhost:3000 >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 后端: http://localhost:8080 >> "%SCRIPTS_DIR%\start-system.bat"
echo echo API文档: http://localhost:8080/swagger-ui.html >> "%SCRIPTS_DIR%\start-system.bat"
echo echo. >> "%SCRIPTS_DIR%\start-system.bat"
echo echo 按任意键继续... >> "%SCRIPTS_DIR%\start-system.bat"
echo pause >> "%SCRIPTS_DIR%\start-system.bat"

REM 创建监控脚本
echo @echo off > "%SCRIPTS_DIR%\monitor-system.bat"
echo title 物业保洁系统 - 系统监控 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo color 0A >> "%SCRIPTS_DIR%\monitor-system.bat"
echo echo 正在检查系统状态... >> "%SCRIPTS_DIR%\monitor-system.bat"
echo echo ================================================ >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo REM 检查Docker状态 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo docker --version ^>nul 2^>^&1 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ❌ Docker未运行 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) else ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ✅ Docker运行正常 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo REM 检查Java >> "%SCRIPTS_DIR%\monitor-system.bat"
echo java -version ^>nul 2^>^&1 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ❌ Java未安装 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) else ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ✅ Java运行正常 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo REM 检查Node.js >> "%SCRIPTS_DIR%\monitor-system.bat"
echo node --version ^>nul 2^>^&1 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ❌ Node.js未安装 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) else ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ✅ Node.js运行正常 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo REM 检查服务端口 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo netstat -ano ^| findstr :3000 ^>nul >> "%SCRIPTS_DIR%\monitor-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ❌ 前端服务未运行 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) else ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ✅ 前端服务运行正常 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo netstat -ano ^| findstr :8080 ^>nul >> "%SCRIPTS_DIR%\monitor-system.bat"
echo if errorlevel 1 ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ❌ 后端服务未运行 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) else ( >> "%SCRIPTS_DIR%\monitor-system.bat"
echo     echo ✅ 后端服务运行正常 >> "%SCRIPTS_DIR%\monitor-system.bat"
echo ) >> "%SCRIPTS_DIR%\monitor-system.bat"
echo. >> "%SCRIPTS_DIR%\monitor-system.bat"
echo echo 按任意键继续... >> "%SCRIPTS_DIR%\monitor-system.bat"
echo pause >> "%SCRIPTS_DIR%\monitor-system.bat"

REM 创建环境检查脚本
echo @echo off > "%SCRIPTS_DIR%\check-env.bat"
echo title 物业保洁系统 - 环境检查 >> "%SCRIPTS_DIR%\check-env.bat"
echo color 0A >> "%SCRIPTS_DIR%\check-env.bat"
echo echo 正在检查系统环境... >> "%SCRIPTS_DIR%\check-env.bat"
echo echo ================================================ >> "%SCRIPTS_DIR%\check-env.bat"
echo. >> "%SCRIPTS_DIR%\check-env.bat"
echo echo 系统信息： >> "%SCRIPTS_DIR%\check-env.bat"
echo systeminfo ^| findstr /B /C:"OS Name" /C:"OS Version" >> "%SCRIPTS_DIR%\check-env.bat"
echo. >> "%SCRIPTS_DIR%\check-env.bat"
echo echo 已安装软件版本： >> "%SCRIPTS_DIR%\check-env.bat"
echo. >> "%SCRIPTS_DIR%\check-env.bat"
echo git --version >> "%SCRIPTS_DIR%\check-env.bat"
echo docker --version >> "%SCRIPTS_DIR%\check-env.bat"
echo java -version >> "%SCRIPTS_DIR%\check-env.bat"
echo node --version >> "%SCRIPTS_DIR%\check-env.bat"
echo npm --version >> "%SCRIPTS_DIR%\check-env.bat"
echo. >> "%SCRIPTS_DIR%\check-env.bat"
echo echo 按任意键继续... >> "%SCRIPTS_DIR%\check-env.bat"
echo pause >> "%SCRIPTS_DIR%\check-env.bat"

REM 创建桌面快捷方式
echo [InternetShortcut] > "%DESKTOP%\启动物业保洁系统.url"
echo URL=file:///%SCRIPTS_DIR%\start-system.bat >> "%DESKTOP%\启动物业保洁系统.url"
echo IconFile=%%SystemRoot%%\system32\SHELL32.dll >> "%DESKTOP%\启动物业保洁系统.url"
echo IconIndex=21 >> "%DESKTOP%\启动物业保洁系统.url"

echo [InternetShortcut] > "%DESKTOP%\系统监控.url"
echo URL=file:///%SCRIPTS_DIR%\monitor-system.bat >> "%DESKTOP%\系统监控.url"
echo IconFile=%%SystemRoot%%\system32\SHELL32.dll >> "%DESKTOP%\系统监控.url"
echo IconIndex=15 >> "%DESKTOP%\系统监控.url"

echo [InternetShortcut] > "%DESKTOP%\环境检查.url"
echo URL=file:///%SCRIPTS_DIR%\check-env.bat >> "%DESKTOP%\环境检查.url"
echo IconFile=%%SystemRoot%%\system32\SHELL32.dll >> "%DESKTOP%\环境检查.url"
echo IconIndex=14 >> "%DESKTOP%\环境检查.url"

echo.
echo 安装完成！
echo ================================================
echo ✅ 所有软件已安装完成
echo.
echo 📁 安装目录: %INSTALL_DIR%
echo 🖥️  桌面快捷方式已创建
echo.
echo ⚠️  重要提醒：
echo 1. 请重启电脑以完成安装
echo 2. 重启后运行桌面上的"启动物业保洁系统"
echo 3. 首次启动可能需要较长时间
echo.
echo 📞 技术支持：
echo 如遇到问题，请运行桌面上的"环境检查"
echo.
pause
echo 安装程序结束，请重启电脑！